package com.custom.base.view

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.Adapter
import android.widget.LinearLayout
import com.custom.base.R

/**
 * 作者：.
 * 时间：2016/12/8.
 * 说明：用LinearLayout绑定listview
 */

class ListViewLinearLayoutView(context: Context, attrs: AttributeSet) :
    LinearLayout(context, attrs) {
    private var orientationa = 1
    init {
        val attributes = context.obtainStyledAttributes(attrs, R.styleable.ListViewLinearLayoutView)
        attributes.apply {
            orientationa = attributes.getInt(R.styleable.ListViewLinearLayoutView_orientation,1)
        }
        orientation = if(orientationa == 1) VERTICAL else HORIZONTAL
        gravity = Gravity.CENTER_VERTICAL
    }


    /**
     * 绑定布局
     */
    fun setAdapter(adapter: Adapter?) {
        adapter?.apply {
            val count = count
            removeAllViews()
            for (i in 0 until count) {
                val v = getView(i, null, null)
                //绑定Adapter的数据到LinearLayout布局
                addView(v)
            }
        }
    }

    /**
     * 添加View
     *
     * @param list
     */
    fun addView(list: List<View>) {
        removeAllViews()
        for (view in list) {
            addView(view)
        }
    }
    /**
     * 设置布局方式
     */
    fun setOrientation(type : OrientationType){
        orientationa = if(type == OrientationType.VERTICAL) 1 else 0
        orientation = if(orientationa == 1) VERTICAL else HORIZONTAL
    }
    enum class OrientationType{
        HORIZONTAL,VERTICAL
    }
}
